package com.atguigu.bigdata.chapter07.window;

import com.atguigu.bigdata.bean.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;

/**
 * @Author lzc
 * @Date 2022/9/4 8:28
 */
public class Flink04_Window_ProcessFunction {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
    
        env
            .socketTextStream("hadoop162", 8888)
            .map(new MapFunction<String, WaterSensor>() {
                @Override
                public WaterSensor map(String value) throws Exception {
                    String[] data = value.split(",");
                    return new WaterSensor(data[0], Long.valueOf(data[1]), Integer.valueOf(data[2]));
                }
            })
            .keyBy(WaterSensor::getId)
            .window(TumblingProcessingTimeWindows.of(Time.seconds(10)))
//            .sum("vc")  // 聚合是放在窗口后,所以聚合是对每个窗口内的元素进行聚合
            .reduce(new ReduceFunction<WaterSensor>() {
                @Override
                public WaterSensor reduce(WaterSensor value1,
                                          WaterSensor value2) throws Exception {
                    System.out.println("xxxx");
                    value1.setVc(value1.getVc() + value2.getVc());
                    return value1;
                }
            })
            .print();
        
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
/*
窗口处理函数:
    
    开窗的目的: 把多个元素放入到窗口内, 然后对这个窗口内的元素做计算, 比如聚合, topN...

增量计算
    简单
        sum
        max min
        maxBy minBy
    
    复杂
        reduce
        
        aggregate


全量计算(窗口关闭的时候才进行开始计算)
    process


 */